package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.NormalOrder;
import com.woniuxy.entity.PickupOrder;
import com.woniuxy.entity.SignOrder;
import com.woniuxy.entity.Waybill;
import com.woniuxy.entity.dto.PickupOrderDTO;
import com.woniuxy.mapper.PickupOrderMapper;
import com.woniuxy.service.PickupOrderService;
import com.woniuxy.util.PageData;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PickupOrderServiceImpl implements PickupOrderService {
    @Resource
    private PickupOrderMapper pickupOrderMapper;
    @Override
    public ResponseData<?> queryPickupOrder(PageData<PickupOrderDTO> pageData) {
        PageHelper.startPage(pageData.getPageNum(),pageData.getPageSize());
        List<PickupOrderDTO> pickupOrders = pickupOrderMapper.queryPickupOrder(pageData.getParam());
        PageInfo pageInfo = new PageInfo(pickupOrders);
        return new ResponseData<>().ok(pageInfo);
    }

    @Override
    public ResponseData<?> tiHuo(String id) { //提货  前端判断状态  提货单id
        //点提货，修改运单 待发车，提货单  已提货
        PickupOrder pickupOrder = pickupOrderMapper.queryByPickId(id);
        String waybillId = pickupOrder.getWaybillId();

        Waybill waybill = pickupOrderMapper.queryWaybill(waybillId);
        String status = waybill.getStatus();
        if("3".equals(status)||"4".equals(status)||"5".equals(status)||"6".equals(status)){
            return new ResponseData<>().fail(ResponseEnum.COMBINED_ORDER_ERROR2);
        }

        pickupOrderMapper.updateWaybillStatus(waybillId);
        pickupOrderMapper.updatePickupOrderStatus(id);
        //提货单号查订单号
        pickupOrderMapper.updateOrderStatus2(pickupOrder.getOrderId());
//
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> handleStart(String id) { //发车
        //修改运单状态,实际发货时间 ，订单表运输中状态
        PickupOrder pickupOrder = pickupOrderMapper.queryByPickId(id);
        String waybillId = pickupOrder.getWaybillId();

        Waybill waybill = pickupOrderMapper.queryWaybill(waybillId);
        String status = waybill.getStatus();
        if ("2".equals(status)||"4".equals(status)||"5".equals(status)||"6".equals(status)){
            return new ResponseData<>().fail(ResponseEnum.COMBINED_ORDER_ERROR2);
        }
        pickupOrderMapper.updateWaybillStatus2(waybillId);
        pickupOrderMapper.updateOrderStatus(pickupOrder.getOrderId());
        return new ResponseData<>().ok();


    }

    @Override
    public ResponseData<?> arrival(String id) { //到站  提货单id
        //修改运单，实际到达时间,
        PickupOrder pickupOrder = pickupOrderMapper.queryByPickId(id);
        String waybillId = pickupOrder.getWaybillId();

        Waybill waybill = pickupOrderMapper.queryWaybill(waybillId);
        String status = waybill.getStatus();
        if ("2".equals(status)||"3".equals(status)||"5".equals(status)||"6".equals(status)){
            return new ResponseData<>().fail(ResponseEnum.COMBINED_ORDER_ERROR2);
        }else {
            pickupOrderMapper.updateWaybillStatus3(waybillId);//修改运单状态和实际到达时间
            //修改司机状态
            pickupOrderMapper.updateDriverStatus(String.valueOf(pickupOrder.getDriverId()));

            //生成签收单
            SignOrder signOrder = new SignOrder();
            signOrder.setCustomerId(pickupOrder.getCustomerId());
            signOrder.setOrderId(pickupOrder.getOrderId());
            signOrder.setPickupAddressId(pickupOrder.getPickupAddressId());
            signOrder.setDeliveryAddressId(pickupOrder.getDeliveryAddressId());
            NormalOrder normalOrder = pickupOrderMapper.queryNormalOrder(pickupOrder.getOrderId());
            signOrder.setOrderStatus(normalOrder.getOrderStatus());
            pickupOrderMapper.addSignOrder(signOrder);
            return new ResponseData<>().ok();
        }
    }


}
